package top.littlejiang.onlinexam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import top.littlejiang.onlinexam.common.utils.BuildTreeUtils;
import top.littlejiang.onlinexam.entity.OlexamMenu;
import top.littlejiang.onlinexam.entity.OlexamMenuRole;
import top.littlejiang.onlinexam.mapper.OlexamMenuMapper;
import top.littlejiang.onlinexam.service.OlexamMenuRoleService;
import top.littlejiang.onlinexam.service.OlexamMenuService;
import top.littlejiang.onlinexam.common.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import top.littlejiang.onlinexam.vo.MenuVO;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * OlexamMenu 服务实现类
 * </p>
 *
 * @author chen
 * @since 2020-12-26
 */
@Service
public class OlexamMenuServiceImpl extends BaseServiceImpl<OlexamMenuMapper, OlexamMenu> implements OlexamMenuService {

    @Autowired
    OlexamMenuRoleService menuRoleService;
    @Autowired
    OlexamMenuMapper menuMapper;

    /**
     * 查询角色id 对应的 权限
     *
     * @param roleId
     * @return Set
     */
    @Override
    public Set<String> listPerms(Integer roleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fk_role_id",roleId);
        wrapper.eq("is_del",0);
        List<OlexamMenuRole> list = menuRoleService.list(wrapper);
        Collection menusId = new HashSet<>();
        list.forEach(item->{
            menusId.add(item.getFkMenuId());
        });

        wrapper.clear();
        wrapper.eq("is_del",0);
        wrapper.select("menu_promise");
        wrapper.in("id",menusId);
        List<OlexamMenu> menuList = this.list(wrapper);
        Set<String> promiess = new HashSet<>();
        menuList.forEach(item->{
            promiess.add(item.getMenuPromise());
        });
        return promiess;
    }

    /**
     * 获取登录用户的菜单列表
     *
     * @param userId
     */
    @Override
    public List<MenuVO> getMenuList(int userId) {
        List<MenuVO> menuVos = BuildTreeUtils.buildList(menuMapper.getMenuList(userId), 0);
        return menuVos;
    }

    /**
     * 获取所有未删除的菜单列表
     *
     * @return
     */
    @Override
    public List<MenuVO> getList() {
        List<MenuVO> menus = BuildTreeUtils.buildList(menuMapper.getList(), 0);
        return menus;
    }

}
